﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Lm.Common.Web.Security
{
    public class UserContext
    {
        public static string Account
        {
            get
            {
                var cookie = HttpContext.Current.Request.Cookies["UserAccount"];
                if (cookie == null)
                {
                    HttpContext.Current.Response.Redirect(System.Configuration.ConfigurationManager.AppSettings["LoginUrl"], true);
                    return string.Empty;
                }
                else
                {
                    try
                    {
                        var key = System.Text.Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["Key"]).Take(32).ToArray();
                        var iv = System.Text.Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["IV"]).Take(16).ToArray();

                        byte[] cookieBytes = new Lm.Common.Security.Cryptography.AesCrypto(key, iv).Decrypt(Convert.FromBase64String(cookie.Value));
                        DateTime expires = DateTime.FromBinary(BitConverter.ToInt64(cookieBytes.Take(8).ToArray(), 0));
                        string userAccount = System.Text.Encoding.UTF8.GetString(cookieBytes.Skip(8).Take(cookieBytes.Length - 8).ToArray());
                        if (expires < DateTime.Now)
                        { throw new Exception("Cookie过期"); }
                        else
                        {
                            UserContext.Account = userAccount;
                            return userAccount;
                        }
                    }
                    catch
                    {
                        HttpContext.Current.Response.Redirect(System.Configuration.ConfigurationManager.AppSettings["LoginUrl"], true);
                        return string.Empty;
                    }
                }
            }
            set
            {
                var key = System.Text.Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["Key"]).Take(32).ToArray();
                var iv = System.Text.Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["IV"]).Take(16).ToArray();

                byte[] expires = BitConverter.GetBytes(DateTime.Now.AddMinutes(Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CookieExpiresMinutes"])).ToBinary());
                byte[] userAccount = System.Text.Encoding.UTF8.GetBytes(value);
                var tmplist = expires.ToList();
                tmplist.AddRange(userAccount.ToList());
                byte[] buf = tmplist.ToArray();
                byte[] tmp = new Lm.Common.Security.Cryptography.AesCrypto(key, iv).Encrypt(buf);
                string str = Convert.ToBase64String(tmp);
                HttpContext.Current.Response.Cookies.Add(new HttpCookie("UserAccount", str));
            }
        }

        public static string IP
        {
            get
            {
                return HttpContext.Current.Request.UserHostAddress;
            }
        }
    }
}